package com.quanshu.exchange.ui.gui.message;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baselibrary.util.glide.GlideUtils;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.UserInterface;
import com.quanshu.exchange.support.db.UserDao;
import com.quanshu.exchange.support.entity.NotifyMsgEntity;
import com.quanshu.exchange.support.entity.UserEntity;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 通知详情
 * Created by xiniu003 on 2017/12/14.
 */

public class NotifyDetailsActivity extends BaseActivity {

    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.notify_details_title)
    TextView notifyDetailsTitle;//页面标题
    @BindView(R.id.notify_details_head_portrait)
    ImageView notifyDetailsHeadPortrait;//头像
    @BindView(R.id.notify_details_user_name)
    TextView notifyDetailsUserName;//昵称
    @BindView(R.id.notify_details_append_message)
    TextView notifyDetailsAppendMessage;//附加的消息
    @BindView(R.id.notify_details_reject)
    TextView notifyDetailsReject;//拒绝
    @BindView(R.id.notify_details_agree)
    TextView notifyDetailsAgree;//同意

    private NotifyMsgEntity notifyMessage;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notify_details);
        ButterKnife.bind(this);
        initIntent();
        initView();
        initListener();
    }

    private void initIntent() {
        notifyMessage = (NotifyMsgEntity) getIntent().getSerializableExtra("NotifyMessage");
    }

    private void initView() {
        if (notifyMessage != null) {
            if (notifyMessage.getApply_type() == 1) {
                notifyDetailsTitle.setText("好友申请");
                GlideUtils.setRoundImageView(this, notifyMessage.getAvatar(), R.drawable.default_avatar, notifyDetailsHeadPortrait);
            } else if (notifyMessage.getApply_type() == 2) {
                notifyDetailsTitle.setText("入群申请");
                GlideUtils.setRoundImageView(this, notifyMessage.getAvatar(), R.drawable.default_avatar, notifyDetailsHeadPortrait);
            }
            notifyDetailsUserName.setText(notifyMessage.getNickname());
            notifyDetailsAppendMessage.setText(notifyMessage.getApply_content());
        }
    }

    private void initListener() {
        RxViewHelper.getInstance().clicks(back, aVoid -> finish());
        RxViewHelper.getInstance().clicks(notifyDetailsAgree, aVoid -> doAgree());
        RxViewHelper.getInstance().clicks(notifyDetailsReject, aVoid -> doClickRefuse());
    }

    private void doAgree() {
        if (notifyMessage.getApply_type() == 1) {
            doAcceptFriendApply();
        } else if (notifyMessage.getApply_type() == 2) {
            doJoinMember2Group();
        }
    }

    /**
     * 同意好友邀请
     */
    private void doAcceptFriendApply() {
        NetClient.getInstance()
                .getNetService(UserInterface.class)
                .addFriend(AppUtils.getSignParams(currentUser.getMemberid(), notifyMessage.getApply_member()), currentUser.getMemberid(), notifyMessage.getApply_member())
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse<UserEntity>>() {
                    @Override
                    public void onSuccess(BaseResponse<UserEntity> response) {
                        UserEntity userEntity = response.getInfo();
                        if (userEntity != null) {
                            UserDao.getInstance().save(userEntity);
                        }
                        finish();
                    }
                });
    }

    private void doJoinMember2Group() {
        NetClient.getInstance()
                .getNetService(UserInterface.class)
                .joinGroup(AppUtils.getSignParams(notifyMessage.getApply_member(), notifyMessage.getHx_groupid()), notifyMessage.getApply_member(), notifyMessage.getHx_groupid())
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        finish();
                    }
                });
    }

    private void doClickRefuse() {
        if (notifyMessage.getApply_type() == 1) {
            doRefuse("1");
        } else if (notifyMessage.getApply_type() == 2) {
            doRefuse("2");
        }
    }

    private void doRefuse(String type) {
        NetClient.getInstance()
                .getNetService(UserInterface.class)
                .rejectApply(AppUtils.getSignParams(currentUser.getMemberid(), notifyMessage.getApply_member(), type),
                        currentUser.getMemberid(), notifyMessage.getApply_member(), type)
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        finish();
                    }
                });
    }

}
